package com.siae.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.AdminMapper;
import com.siae.pojo.Admin;
import com.siae.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public String login(Map dataMap) {
        String adminName = dataMap.get("adminName").toString();
        String password = dataMap.get("password").toString();
//        Result<Admin> result = new Result<>();
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_name",adminName);
        List<Admin> admins = adminMapper.selectList(wrapper);
        if (admins.size() == 0){
            return "1";
        }
        for (Admin admin : admins) {
            if(admin.getPassword().equals(password)){
                StpUtil.login(admin.getId());
                return StpUtil.getTokenValue();
            }
        }
        return "2";
    }
}
